<?php
/*
    Copyright (C) 2008 - 2010 Inanta Martsanto
    
    This file is part of Koma.

    Koma is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Koma is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Koma.  If not, see <http://www.gnu.org/licenses/>.
    
    Inanta Martsanto (inanta@inationsoft.com)
*/

try
{
    if(!@file_exists(KOMA_ROOT_PATH.'/routines.php')) die('Open the mainfile.php and changes KOMA_ROOT_PATH and KOMA_URL definition to correct path. Set KOMA_ROOT_PATH to your website folder (example: "C:\\htdocs") and set KOMA_URL to your website URL (example: "http://localhost").');
    
    define('KOMA_ROLE_ADMIN', '1');
    define('KOMA_ROLE_USERS', '2');
    define('KOMA_ROLE_ANONYMOUS', '3');
    
    define('KOMA_SMARTY_PATH', KOMA_ROOT_PATH.'/classes/extclasses/Smarty-2.6.20');
    define('KOMA_COMPILE_PATH', KOMA_ROOT_PATH.'/templates_c');
    define('KOMA_CACHE_PATH', KOMA_ROOT_PATH.'/cache');
    define('KOMA_THEME_PATH', KOMA_ROOT_PATH.'/themes');
		    
    define('KOMA_UPLOAD_PATH', KOMA_ROOT_PATH.'/uploads');
    define('KOMA_CLASS_PATH', KOMA_ROOT_PATH.'/classes');
    define('KOMA_INCLUDE_PATH', KOMA_ROOT_PATH.'/includes');
    define('KOMA_LOCALE_PATH', KOMA_ROOT_PATH.'/locales');
    
    define('KOMA_THEME_URL', KOMA_URL.'/themes');
        
    define('KOMA_DEFAULT_CHARSET', 'UTF-8');
    define('KOMA_DEFAULT_LOCALE', 'en_US');
    define('KOMA_DEFAULT_THEME', 'basic');
	
    define('KOMA_DEFAULT_SITE_TITLE', 'Koma Site');
    define('KOMA_DEFAULT_SITE_SLOGAN', 'Think object, think reuse, everybody can think different');
    define('KOMA_DEFAULT_META_KEY', 'Koma, Web Framwork, CMS, Content Management System, Inationsoft');
    define('KOMA_DEFAULT_META_DESC', 'Koma is Konten Manajemen Web Framework');
    define('KOMA_DEFAULT_META_AUTHOR', 'Inationsoft');
    define('KOMA_DEFAULT_META_COPYRIGHT', '&copy; 2008 - 2010 Inationsoft');
    define('KOMA_DEFAULT_META_GENERATOR', 'KOMA');
    
    define('KOMA_DEFAULT_FAVICON', KOMA_URL.'/favicon.ico');
    
    include_once(KOMA_CLASS_PATH.'/KomaClassMapper.class.php');

    function __autoload($classname)
    {
        $mapper = KomaClassMapper::getInstance();
        
        if ($mapper->getClassPath($classname))
        {
            include_once($mapper->getClassPath($classname));
        }
    }
    
    $komaDependencies = array('');
    $komaRoutines = KomaRoutines::getInstance();
    
    if(function_exists('shutdown')) register_shutdown_function('shutdown');
    else throw new KomaFatalErrorException(array('shutdown'));

    if(!function_exists('show_message') || !function_exists('redirect') || !class_exists('KomaTemplate'))
    {
        throw new KomaFatalErrorException(array('show_message', 'redirect', 'KomaTemplate'));
    }
    
    // move to somewhere else
    //if(is_writeable(KOMA_ROOT_PATH.'/mainfile.php')) throw new KomaWriteableException(array('name' => KOMA_ROOT_PATH.'/mainfile.php'));
    //
    
    $komaDB = KomaDatabaseFactory::getInstance(KOMA_DB_TYPE);
    $komaSession = KomaSessionHandler::getInstance();
    $komaConfig = KomaConfigHandler::getInstance();
    $komaUserHandler = new KomaUserHandler($_SESSION['UserId']);
    
    define('KOMA_SITE_TITLE', $komaConfig->getVar('Title') ? $komaConfig->getVar('Title') : KOMA_DEFAULT_SITE_TITLE);
    define('KOMA_SITE_SLOGAN', $komaConfig->getVar('Slogan') ? $komaConfig->getVar('Slogan') : KOMA_DEFAULT_SITE_TITLE);
    
    define('KOMA_CHARSET', $komaConfig->getVar('Charset') ? $komaConfig->getVar('Charset') : KOMA_DEFAULT_CHARSET);
    
    if(isset($_GET['locale'])) $_SESSION['Locale'] = $_GET['locale'];
    define('KOMA_LOCALE', isset($_SESSION['Locale']) ? $_SESSION['Locale'] : (($komaConfig->getVar('Locale') ? $komaConfig->getVar('Locale') : KOMA_DEFAULT_LOCALE)));

    if(isset($_GET['theme']))
    {
        $themes = KomaLists::getInstalledThemes();
        
        if(isset($themes[$_GET['theme']]))
        {
            $_SESSION['Theme'] = $_GET['theme'];
        }
        
    }

    define('KOMA_THEME', isset($_SESSION['Theme']) ? $_SESSION['Theme'] : (($komaConfig->getVar('Theme') ? $komaConfig->getVar('Theme') : KOMA_DEFAULT_THEME)));
    
    $komaRoutines->loadLanguage(KOMA_LOCALE);
    
    //if($komaConfig->getVar('IsOffline') && $komaUserHandler->getVar('rid') != KOMA_ROLE_ADMIN) show_message(KOMA_URL, T_('This site is temporary unvailable due to maintenance. Please come back later.'));
    
    if(file_exists('./args.php') || file_exists('./../args.php')) include(KOMA_ROOT_PATH.'/presentation.php');
}
catch(Exception $ex)
{
    $trace = $ex->getTrace();
    
    show_message(sprintf(T_('The page cannot be displayed due to internal error.<br/>If you administrator of this site please visit <a href=%s>KOMA official site</a> for assistance'), '"'.INATIONSOFT_SITE.'"'),
                 sprintf(T_('KOMA Error Message: %s<br/>File: %s line %s'),
                         $ex->getMessage(), str_replace(KOMA_ROOT_PATH.'/', "", $trace[1]['file']), $trace[1]['line']));
}
?>